package com.hsd.znsh.repository.impl;


import com.hsd.znsh.repository.BaseRepository;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.util.Iterator;
import java.util.List;
import java.util.Map;


@Repository
public class BaseRepositoryImpl implements BaseRepository {

    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession() {
        return this.sessionFactory.getCurrentSession();
    }

    @Override
    public <T> T get(String hql, Object object) {
        List<T> ls = find(hql,object);
        if (ls != null && ls.size() > 0) {
            return ls.get(0);
        }
        return null;
    }

    @Override
    public <T> T get(String hql, Map<String, Object> params) {
        List<T> ls = find(hql,params);
        if (ls != null && ls.size() > 0) {
            return ls.get(0);
        }
        return null;
    }

    @Override
    public <T> List<T> find(String hql, Object object) {
        Query query = this.getCurrentSession().createQuery(hql);
        query.setProperties(object);
        return query.list();
    }


    @Override
    public <T> List<T> find(String hql, Map<String, Object> params) {
        Query query = this.getCurrentSession().createQuery(hql);
        query.setProperties(params);
        return query.list();
    }

    @Override
    public <T> List<T> find(String hql, Object object, Pageable pageable) {
        Sort sort=pageable.getSort();
        if(sort!=null){
            hql+=" order by ";
            Iterator<Sort.Order> iterator=sort.iterator();
            while(iterator.hasNext()){
                Sort.Order order=iterator.next();
                hql+=order.getProperty()+" "+order.getDirection()+",";
            }
            hql=hql.substring(0,hql.length()-1);
        }
        int size=pageable.getPageSize();
        int pageNumber=pageable.getPageNumber();
        Query query = this.getCurrentSession().createQuery(hql);
        query.setProperties(object);
        return query.setFirstResult(size * pageNumber).setMaxResults(size).list();
    }


    @Override
    public <T> List<T> find(String hql, Map<String, Object> params, Pageable pageable) {
        Sort sort=pageable.getSort();
        if(sort!=null){
            hql+=" order by ";
            Iterator<Sort.Order> iterator=sort.iterator();
            while(iterator.hasNext()){
                Sort.Order order=iterator.next();
                hql+=order.getProperty()+" "+order.getDirection()+",";
            }
            hql=hql.substring(0,hql.length()-1);
        }
        int size=pageable.getPageSize();
        int pageNumber=pageable.getPageNumber();
        Query query = this.getCurrentSession().createQuery(hql);
        query.setProperties(params);
        return query.setFirstResult(size * pageNumber).setMaxResults(size).list();
    }

    @Override
    public long count(String hql, Object object) {
        System.out.println(hql);
        Query query = this.getCurrentSession().createQuery(hql);
        query.setProperties(object);
        return (long) query.uniqueResult();
    }

    @Override
    public long count(String hql, Map<String, Object> params) {
        Query query = this.getCurrentSession().createQuery(hql);
        query.setProperties(params);
        return (long) query.uniqueResult();
    }

}
